/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.halfbit.pinnedsection.slice;

import de.halfbit.pinnedsection.HiLogs;
import de.halfbit.pinnedsection.IClickedListener;
import de.halfbit.pinnedsection.PinnedSection;
import de.halfbit.pinnedsection.PinnedSectionListView;
import de.halfbit.pinnedsection.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice extends AbilitySlice implements IClickedListener
 *
 * @author twx1009780
 * @since 2021-01-22
 */
public class MainAbilitySlice extends AbilitySlice implements IClickedListener{
    PinnedSectionListView pinnedSectionView;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        pinnedSectionView = (PinnedSectionListView) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_main, null, false);
        super.setUIContent(pinnedSectionView);
        initView();
    }

    private void initView() {
        String[] countryArray = getStringArray(ResourceTable.Strarray_countries);
        List<PinnedSection> dataList = new ArrayList<>();
        for (int pos = 0; pos <= countryArray.length - 1; pos++) {
            PinnedSection pinnedData = new PinnedSection();
            pinnedData.setText(countryArray[pos]);
            pinnedData.setHeader(false);
            dataList.add(pinnedData);
        }
        pinnedSectionView.setHeaderSticky(true);
        pinnedSectionView.setRandomColorHeader(true);
        pinnedSectionView.setDataItems(dataList, "Total Count : "+dataList.size());

        pinnedSectionView.registerClickListner(this);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        pinnedSectionView.unregisterClickListner();
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int position, long count) {
        HiLogs.d("List Item Clicked");
    }

    @Override
    public void onStickyHeaderClicked(Component component) {
        HiLogs.d("Sticky Header Clicked");
    }
}